#include "stdafx.h"
#include "CharConvert.h"
#include <strsafe.h>

// UTF16 -> ANSI
CUnicodeToAnsi::CUnicodeToAnsi()
{
    m_iOut            = 0;
	m_pStrPtr         = NULL;
    m_bNullTerminated = TRUE;
}

CUnicodeToAnsi::CUnicodeToAnsi(LPCWSTR pszIn, const BOOL& bNullTerminated)
{
	m_pStrPtr         = NULL;
    m_bNullTerminated = bNullTerminated;

    Convert(pszIn, bNullTerminated);
}

CUnicodeToAnsi::~CUnicodeToAnsi()
{
	if(m_pStrPtr)
    {
		delete [] (STREAM *)m_pStrPtr;
        m_pStrPtr = NULL;
    }
}

void CUnicodeToAnsi::operator=(LPCWSTR pszIn)
{
	Convert(pszIn, m_bNullTerminated);
}

const char* CUnicodeToAnsi::Convert(LPCWSTR pszIn, const BOOL& bNullTerminated)
{
    if(m_pStrPtr)
    {
	    delete [] (STREAM *)m_pStrPtr;
    }

    m_bNullTerminated = bNullTerminated;

    if(pszIn == NULL || pszIn[0] == '\0') 
	{
		m_pStrPtr = NULL;
        m_iOut    = 0;
		return GetPtr();
	}
	
	m_iOut = CUnicodeMethod::UnicodeToAnsi((STREAM *)pszIn, -1, NULL, 0, 1, 
                                           m_bNullTerminated);

    if(m_iOut > 0)
	{
        STREAM * pStreamID = new STREAM[m_iOut];
		m_iOut = CUnicodeMethod::UnicodeToAnsi((STREAM *)pszIn, -1, pStreamID, 
                                               m_iOut, 1, m_bNullTerminated);
		m_pStrPtr = (LPCSTR)pStreamID;
	}
	else
	{
		m_pStrPtr = NULL;
	}

    return GetPtr();
}


// ANSI -> UTF16
CAnsiToUnicode::CAnsiToUnicode()
{
    m_iOut            = 0;
	m_pStrPtr         = NULL;
    m_bNullTerminated = TRUE;
}

CAnsiToUnicode::CAnsiToUnicode(const char * pszIn, const BOOL& bNullTerminated)
{
	m_pStrPtr         = NULL;
    m_bNullTerminated = bNullTerminated;

    Convert(pszIn, bNullTerminated);
}

CAnsiToUnicode::~CAnsiToUnicode()
{
	if(m_pStrPtr)
    {
		delete [] (STREAM *)m_pStrPtr;
        m_pStrPtr = NULL;
    }
}

void CAnsiToUnicode::operator=(const char * pszIn)
{
	Convert(pszIn, m_bNullTerminated);
}

LPCWSTR CAnsiToUnicode::Convert(const char * pszIn, const BOOL& bNullTerminated)
{
    if(m_pStrPtr)
    {
	    delete [] (STREAM *)m_pStrPtr;
    }

    m_bNullTerminated = bNullTerminated;

	if(pszIn == NULL || pszIn[0] == '\0') 
	{
		m_pStrPtr = NULL;
        m_iOut    = 0;
		return GetPtr();
	}

	m_iOut = CUnicodeMethod::AnsiToUnicode((STREAM *)pszIn, -1, NULL, 0, 1, 
                                           m_bNullTerminated);
	
	if(m_iOut > 0)
	{
		STREAM * pStreamID = new STREAM[m_iOut];
		::ZeroMemory(pStreamID, m_iOut);	
		m_iOut = CUnicodeMethod::AnsiToUnicode((STREAM *)pszIn, -1, pStreamID, 
                                               m_iOut, 1, m_bNullTerminated);
		m_pStrPtr = (LPCWSTR)pStreamID;
	}
	else
	{
		m_pStrPtr = NULL;
	}

    return GetPtr();
}


// UTF16 -> UTF8
CUTF16ToUTF8::CUTF16ToUTF8()
{
    m_iOut            = 0;
	m_pStrPtr         = NULL;
    m_bNullTerminated = TRUE;
}

CUTF16ToUTF8::CUTF16ToUTF8(LPCWSTR pszIn, const BOOL& bNullTerminated)
{
	m_pStrPtr         = NULL;
    m_bNullTerminated = bNullTerminated;

    Convert(pszIn, bNullTerminated);
}

CUTF16ToUTF8::~CUTF16ToUTF8()
{
	if(m_pStrPtr)
    {
		delete [] (STREAM *)m_pStrPtr;
        m_pStrPtr = NULL;
    }
}

void CUTF16ToUTF8::operator=(LPCWSTR pszIn)
{
	Convert(pszIn, m_bNullTerminated);
}

const char * CUTF16ToUTF8::Convert(LPCWSTR pszIn, const BOOL& bNullTerminated)
{
    if(m_pStrPtr)
    {
	    delete [] (STREAM *)m_pStrPtr;
    }

    m_bNullTerminated = bNullTerminated;

	if(pszIn == NULL || pszIn[0] == '\0')
	{
		m_pStrPtr = NULL;
        m_iOut    = 0;
		return GetPtr();
	}

	m_iOut = CUnicodeMethod::UTF16ToUTF8((STREAM *)pszIn, -1, NULL, 0, m_bNullTerminated);
	
	if(m_iOut > 0)
	{
        STREAM * pStreamID = new STREAM[m_iOut * sizeof(TCHAR)];
        ::ZeroMemory(pStreamID, m_iOut * sizeof(TCHAR));
		m_iOut = CUnicodeMethod::UTF16ToUTF8((STREAM *)pszIn, -1, pStreamID, 
                                             m_iOut, m_bNullTerminated);
		
		m_pStrPtr = (LPCSTR)pStreamID;
	}
	else
	{
		m_pStrPtr = NULL;
	}

    return GetPtr();
}


// UTF8 -> UTF16
CUTF8ToUTF16::CUTF8ToUTF16()
{
    m_iOut            = 0;
	m_pStrPtr         = NULL;
    m_bNullTerminated = TRUE;
}

CUTF8ToUTF16::CUTF8ToUTF16(const char * pszIn, const BOOL& bNullTerminated)
{
	m_pStrPtr         = NULL;
    m_bNullTerminated = bNullTerminated;

    Convert(pszIn, bNullTerminated);
}

CUTF8ToUTF16::~CUTF8ToUTF16()
{
    if(m_pStrPtr)
    {
	    delete [] (STREAM *)m_pStrPtr;
        m_pStrPtr = NULL;
    }
}

void CUTF8ToUTF16::operator=(const char * pszIn)
{
	Convert(pszIn, m_bNullTerminated);
}

LPCWSTR CUTF8ToUTF16::Convert(const char * pszIn, const BOOL& bNullTerminated)
{
    if(m_pStrPtr)
    {
	    delete [] (STREAM *)m_pStrPtr;
    }

    m_bNullTerminated = bNullTerminated;

	if(pszIn == NULL || pszIn[0] == '\0')
	{
		m_pStrPtr = NULL;
        m_iOut    = 0;
		return GetPtr();
	}

	m_iOut = CUnicodeMethod::UTF8ToUTF16((STREAM *)pszIn, -1, NULL, 0, m_bNullTerminated);
	if(m_iOut > 0)
	{
		STREAM * pStreamID = new STREAM[m_iOut];
		m_iOut = CUnicodeMethod::UTF8ToUTF16((STREAM *)pszIn, -1, pStreamID, 
                                             m_iOut, m_bNullTerminated);
		m_pStrPtr = (LPCWSTR)pStreamID;
	}
	else
	{
		m_pStrPtr = NULL;
	}

    return GetPtr();
}